package com.student.service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import java.util.Random;

import com.student.DBConn;
import com.student.entities.CommentCount;
import com.student.entities.CommentEntity;

public class CommentService {

	public static List<CommentEntity> getComment(String goods_id, String start,
			String count) throws SQLException {

		List<CommentEntity> commentList = new ArrayList<CommentEntity>();

		Connection conn = DBConn.getConn();
		Statement stmt = conn.createStatement();
		String sql = "select * from shop_comment , shop_user where comment_userId = user_id and comment_goodsId = "
				+ goods_id
				+ " order by comment_time desc  limit "
				+ start
				+ "  , " + count;

		ResultSet rs = stmt.executeQuery(sql);

		while (rs.next()) {
			CommentEntity comment = new CommentEntity();
			DateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
			comment.setContent(rs.getString("comment_content"));
			comment.setGoods_id(rs.getInt("comment_goodsId"));
			comment.setRank(rs.getInt("comment_rank"));
			comment.setTime(format.format(rs.getDate("comment_time")));
			comment.setUser_id(rs.getInt("comment_userId"));
			comment.setUser_name(rs.getString("user_name"));
			commentList.add(comment);
		}
		rs.close();
		stmt.close();
		conn.close();
		System.out.println(commentList);
		return commentList;
	}

	public static CommentCount getCommentCount(String goods_id)
			throws SQLException {
		CommentCount comC = new CommentCount();
		Connection conn = DBConn.getConn();
		Statement stmt = conn.createStatement();
		String sql = "select comment_goodsId,comment_rank,count(*) as comment_count from shop_comment  where  comment_goodsId = "
				+ goods_id + " group by comment_rank";

		ResultSet rs = stmt.executeQuery(sql);

		while (rs.next()) {
			switch (rs.getInt("comment_rank")) {
			case 1:
				comC.setStar1(rs.getInt("comment_count"));
				break;
			case 2:
				comC.setStar2(rs.getInt("comment_count"));
				break;
			case 3:
				comC.setStar3(rs.getInt("comment_count"));
				break;
			case 4:
				comC.setStar4(rs.getInt("comment_count"));
				break;
			case 5:
				comC.setStar5(rs.getInt("comment_count"));
				break;
			default:
				break;
			}
			comC.setAll_star();
		}
		rs.close();
		stmt.close();
		conn.close();
		System.out.println(comC);
		return comC;
	}

	public static void insert() throws SQLException {
		Connection conn = DBConn.getConn();
		Statement stmt = conn.createStatement();
		String[] str = { "不错的样子，就是有点贵哦", "太难吃了~~~", "肉都冒得，命苦咧",
				"好吃，哈哈，比学校食堂好吃多了，哈哈···", "好吃，哈哈，下次再来···" };
		for (int i = 24; i < 50; i++) {
			String sql = "INSERT INTO `shop_comment` VALUES ('" + i
					+ "', '1', '" + (new Random().nextInt(4) + 1)
					+ "', '好评', '" + str[new Random().nextInt(5)] + "', '"
					+ (new Random().nextInt(5) + 1) + "', '2012-10-"
					+ (i % 30 + 1) + " 22:25:28', '0')";

			stmt.execute(sql);

		}
		stmt.close();
		conn.close();
	}

	public static void main(String args[]) {
		try {
			// insert();
			System.out.println(getComment(1 + "", 50 + "", 10 + ""));
			System.out.println("结束了-----------");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
